use crate::core::c::{MAINLAND_API_DOMAIN, OVERSEAS_API_DOMAIN};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use strum::{AsRefStr, EnumIter, EnumString};

/// APP上架的渠道
///
/// 客户端会为每个渠道维护一个包，不同渠道设置不会相互影响
#[derive(
    Deserialize,
    Serialize,
    Eq,
    PartialEq,
    Hash,
    Default,
    Debug,
    Copy,
    Clone,
    EnumString,
    AsRefStr,
    EnumIter,
)]
pub enum Channel {
    #[default]
    ForceLogin,
    QooApp,
    GooglePlay,
    AppStore,
    TapTapMainland,
    BiliBiliMainland,
    AppStoreMainland,
}

impl From<String> for Channel {
    fn from(value: String) -> Self {
        Self::from_str(&value).unwrap()
    }
}

impl Display for Channel {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_ref())
    }
}

impl Channel {
    pub fn get_api_domain(&self) -> &str {
        match self {
            Channel::ForceLogin => MAINLAND_API_DOMAIN,
            Channel::QooApp => OVERSEAS_API_DOMAIN,
            Channel::GooglePlay => OVERSEAS_API_DOMAIN,
            Channel::AppStore => OVERSEAS_API_DOMAIN,
            Channel::TapTapMainland => MAINLAND_API_DOMAIN,
            Channel::BiliBiliMainland => MAINLAND_API_DOMAIN,
            Channel::AppStoreMainland => MAINLAND_API_DOMAIN,
        }
    }
}
